package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.AdminStorageService;
import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.domain.MarketStorageExample;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminStorageServiceImpl implements AdminStorageService {
    @Autowired
    private MarketStorageMapper storageMapper;
    // 查询
    @Override
    public List<MarketStorage> list(Integer page, Integer limit, String sort, String order, String key, String name) {
        // 创建实例对象
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        // 设置排序
        marketStorageExample.setOrderByClause(sort +" "+ order);
        // 添加查询条件
        MarketStorageExample.Criteria criteria = marketStorageExample.or().andLogicalDeleted(true);
        if (key != null) {
            criteria.andKeyLike("%" + key + "%");
        }
        if (name != null) {
            criteria.andKeyLike("%" + name + "%");
        }
        // 添加分页
        PageHelper.startPage(page, limit);
        List<MarketStorage> marketStorages = storageMapper.selectByExample(marketStorageExample);
        // 收尾
        
        return marketStorages;
    }

    // 更新
    @Override
    public MarketStorage update(MarketStorage updateMarketStorage) {
        // 更新数据
        int i = storageMapper.updateByPrimaryKeySelective(updateMarketStorage);
        // 根据最后插入的id，获取插入的数据
        MarketStorage marketStorage = storageMapper.selectByPrimaryKey(i);
        
        
        return marketStorage;
    }

    @Override
    public boolean delete(MarketStorage marketStorage) {
        // 设定为逻辑删除
        marketStorage.setDeleted(true);
        // 更新数据
        int i = storageMapper.updateByPrimaryKeySelective(marketStorage);
        // 收尾
        
        
        // 返回判定结果
        return i > 0;
    }

    // 创建
    @Override
    public int create(MarketStorage marketStorage) {
        // 创建示例对象
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        int id = storageMapper.insert(marketStorage);
        // 收尾
        
        
        return id;
    }


}
